package repository

import (
	"boy-go/modules/system/model"
	"boy-go/modules/system/vo"
	"boy-go/pkg/db"
	"boy-go/pkg/request"
	"gorm.io/gorm"
	"time"
)

type SysConfigRepository struct {
	DB *gorm.DB
}

func NewSysConfigRepository() *SysConfigRepository {
	return &SysConfigRepository{
		DB: db.GetMasterDB(),
	}
}
func (r *SysConfigRepository) GetById(configId int64) (*model.SysConfig, error) {
	db := r.DB
	m := &model.SysConfig{}
	err := db.Model(&model.SysConfig{}).Where("config_id = ?", configId).First(m).Error
	if err != nil {
		return nil, err
	}
	return m, nil
}

func (r *SysConfigRepository) GetByKey(configKey string) (*model.SysConfig, error) {
	db := r.DB
	m := &model.SysConfig{}
	err := db.Model(&model.SysConfig{}).Where("config_key = ?", configKey).First(m).Error
	if err != nil {
		return nil, err
	}
	return m, nil
}

func (r *SysConfigRepository) List(query *vo.QuerySysConfigReq, pageReq *request.PageRequest) ([]*model.SysConfig, int64, error) {
	tx := r.DB.Model(&model.SysConfig{})
	if query != nil {
		if query.ConfigName != nil {
			tx = tx.Where("config_name like ?", "%"+*query.ConfigName+"%")
		}
		if query.ConfigKey != nil {
			tx = tx.Where("config_key like ?", "%"+*query.ConfigKey+"%")
		}
		if query.ConfigValue != nil {
			tx = tx.Where("config_value = ?", *query.ConfigValue)
		}
		if query.ConfigType != nil {
			tx = tx.Where("config_type = ?", *query.ConfigType)
		}
		if query.Remark != nil {
			tx = tx.Where("remark = ?", *query.Remark)
		}
		if query.Params != nil {
			val, have := query.Params["beginTime"]
			if have {
				beginTime := val.(string)
				tx = tx.Where("date_format(create_time,'%Y%m%d') >= date_format('" + beginTime + "','%Y%m%d')")
			}
			val, have = query.Params["endTime"]
			if have {
				endTime := val.(string)
				tx = tx.Where("date_format(create_time,'%Y%m%d') <= date_format('" + endTime + "','%Y%m%d')")
			}
		}
	}
	list := make([]*model.SysConfig, 0)
	if pageReq != nil {
		offSet := 0
		limit := 10
		if pageReq != nil {
			if pageReq.PageNum > 0 {
				offSet = (pageReq.PageNum - 1) * limit
			}
			if pageReq.PageSize > 0 {
				limit = pageReq.PageSize
			}
		}
		var total int64
		if err := tx.Count(&total).Error; err != nil {
			return nil, 0, err
		}
		if total > 0 {
			if err := tx.Select("*").Offset(offSet).Limit(limit).Find(&list).Error; err != nil {
				return nil, total, err
			}
		}
		return list, total, nil
	} else {
		if err := tx.Select("*").Find(&list).Error; err != nil {
			return nil, 0, err
		}
	}
	return list, int64(len(list)), nil
}

// Save 添加
func (r *SysConfigRepository) Save(m *model.SysConfig) error {
	currentTime := time.Now()
	m.CreateAt = &currentTime
	return r.DB.Model(&model.SysConfig{}).Create(m).Error
}

// Update 更新
func (r *SysConfigRepository) Update(m *model.SysConfig) error {
	currentTime := time.Now()
	m.UpdateAt = &currentTime
	return r.DB.Model(&model.SysConfig{}).Where("config_id = ?", m.ConfigId).Omit("config_id").Updates(m).Error
}

// Delete 删除
func (r *SysConfigRepository) Delete(configId int64) error {
	return r.DB.Model(&model.SysConfig{}).Where("config_id = ?", configId).Delete(&model.SysConfig{}).Error
}

// DeleteBatch 批量删除
func (r *SysConfigRepository) DeleteBatch(ids []string) error {
	db := r.DB
	db = db.Where("config_id in ?", ids)
	return db.Delete(&model.SysConfig{}).Error
}
